package com.yanggu.mapreduce.combinesmallfile;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.CombineFileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.CombineTextInputFormat;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

/**
 * @version V1.0
 * @author: YangGu
 * @date: 2019/12/14 23:56
 * @description:
 */
public class CombineSmallFileDriver {

    public static void main(String[] args) throws Exception {

        args = new String[2];
        args[0] = "input/combinesmallfile";
        args[1] = "output/combinesmallfile";

        //1. 获得job对象
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf);

        //2. 关联mapper、reducer、driver类
        job.setMapperClass(CombineSmallFileMapper.class);
        job.setReducerClass(CombineSmallFileReducer.class);
        job.setJarByClass(CombineSmallFileDriver.class);

        //3. 设置mapper的key和value的输出类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);

        //4. 设置最终的输出的key和value的类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        //设置FileInputFormat的类型
        //如果不设置InputFormat，它默认用的是TextInputFormat.class
        job.setInputFormatClass(CombineTextInputFormat.class);
        //设置虚拟存储切片的最大值
        CombineTextInputFormat.setMaxInputSplitSize(job, 20971520);

        //5. 设置输入和输出的路径
        FileInputFormat.setInputPaths(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));

        //6. 提交job。并关闭程序
        boolean result = job.waitForCompletion(true);
        System.exit(result ? 0 : 1);
    }

}
